package com.paradisecloudtech.vc.system.mvp.presenter;

import com.common.library.base.BasePresenter;
import com.common.library.base.BaseView;
import com.paradisecloudtech.vc.system.mvp.bean.ConfsBean;
import com.paradisecloudtech.vc.system.mvp.model.ConfsModel;
import com.paradisecloudtech.vc.system.mvp.view.ConfsView;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public class ConfsPresenter implements BasePresenter {

    private ConfsModel confsModel;
    private ConfsView confsView;
    private CompositeDisposable compositeDisposable;

    public ConfsPresenter() {
        confsModel = new ConfsModel();
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void start() {

    }

    @Override
    public void attachView(BaseView view) {
        confsView = (ConfsView) view;
    }

    @Override
    public void detachView() {
        confsView = null;
    }

    @Override
    public boolean isViewDetach() {
        return !(this.confsView == null);
    }

    @Override
    public void stop() {
        if (!compositeDisposable.isDisposed()) {
            compositeDisposable.dispose();
        }
    }


    public void requestConfsData(int status) {
        if (isViewDetach()) {
            confsView.startGetConferences();
        }
        confsModel.requestConfsData(status)
                .subscribe(new Observer<ConfsBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(ConfsBean confsBean) {
                        if (isViewDetach()) {
                            confsView.getConferencesSuccess(confsBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isViewDetach()) {
                            confsView.requestFailedByNet(e);
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (isViewDetach()) {
                            confsView.endGetConferences();
                        }
                    }
                });
    }

}
